onreadystatechange   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 0
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
1
var valName = 1;
2
var valEmail = 1;
3
var valUser = 1;
4
var valMob = 1;
5
var valPassRegister = 1;
6
var errorText = '{"font-size":"12px","color":"#a94442","display":"inline-block","padding": "5px","font-weight": "700"}';
7
errorText = JSON.parse(errorText);
8
var errorInput = '{"outline":"none","border-color":"#a94442"}';
9
errorInput = JSON.parse(errorInput);
10
var removeError = '{"outline":"none","border-color":"#ccc"}';
11
removeError = JSON.parse(removeError);
12
13
function showError(key, value)
14
{
15
    key = "#"+key;
16
    var selector = "input"+key;
17
    $(selector).prev("span").remove();
18
    $(key).css(errorInput);
19
    var txt = $("<span></span>").text(value).css(errorText);
20
    $(key).before(txt);
21
}
22
23
function validateEmail(val)
24
{
25
    var re = /^\S+@\w+\.\w+$/;
26
    return re.test(val);
27
}
28
29
function name()
30
{
31
    var name = $("#name").val();
32
    $("#name + span").text("");
33
    if(name === "")
34
    {
35
        valName = 1;
36
        showError("name", " *Please input your name");
37
    }
38
    else
39
    {
40
        $("#name").css(removeError);
41
        valName = 0;
42
    }
43
}
44
45
function email()
46
{
47
    var val = $("#email").val();
48
    var ret = validateEmail(val);
49
    $("input#email").prev("span").remove();
50
    if(val === "")
51
    {
52
        valEmail = 1;
53
        showError("email", " *Enter Your email address");
54
    }
55
    else if(!ret)
56
    {
57
        valEmail = 1;
58
        showError("email", " *Invalid Email");
59
    }
60
    else
61
    {
62
        $("#email").css(removeError);
63
        valEmail = 0;
64
    }
65
}
66
67
function username()
68
{
69
    var val = $("#username").val();
70
    var re = /^\S+@/;
71
72
    $("input#username").prev("span").remove();
73
    if(val === "")
74
    {
75
        valUser = 1;
76
        showError("username", " *Enter Your username");
77
    }
78
    else if(re.test(val))
79
    {
80
        valUser = 1;
81
        showError("username", " *Invalid username");
82
    }
83
    else
84
    {
85
        $("#username").css(removeError);
86
        valUser = 0;
87
    }
88
}
89
90
function mob()
91
{
92
    var mob = $("#mob").val();
93
    var re = /^[0-9]{10}$/;
94
    $("input#mob").prev("span").remove();
95
    if(mob === "")
96
    {
97
        valMob = 1;
98
        showError("mob", " *Enter your mobile no.");
99
    }
100
    else if(!re.test(mob))
101
    {
102
        valMob = 1;
103
        showError("mob", " *Enter 10 digit mobile no.");
104
    }
105
    else
106
    {
107
        $("#mob").css(removeError);
108
        valMob = 0;
109
    }
110
}
111
112
function passwordRegister()
113
{
114
    var pass = $("#passRegister").val();
115
    $("input#passRegister").prev("span").remove();
116
    if(pass === "")
117
    {
118
        valPassRegister = 1;
119
        showError("passRegister", " *Enter your password");
120
    }
121
    else
122
    {
123
        $("#passRegister").css(removeError);
124
        valPassRegister = 0;
125
    }
126
}
127
128
function initRegister()
129
{
130
    name();
131
    email();
132
    username();
133
    mob();
134
    passwordRegister();
135
}
136
137
// Name validation
138
139
$("#name").blur(function()
140
{
141
    name();
142
});
143
144
// Email validation
145
146
$("#email").keyup(function() {
147
    email();
148
});
149
150
$("#email").blur(function() {
151
    email();
152
});
153
154
155
// username validation
156
157
$("#username").blur(function() {
158
    username();
159
});
160
161
// Mobile validation
162
163
$("#mob").blur(function() {
164
    mob();
165
});
166
167
//Password validation
168
169
$("#passRegister").blur(function() {
170
    passwordRegister();
171
172
});
173
174
function registerCheck() {
175
    var name = $("#name").val();
176
    var email = $("#email").val();
177
    var username = $("#username").val();
178
    var mob = $("#mob").val();
179
    var password = $("#passRegister").val();
180
181
    initRegister();
182
183
    if(valName === 0 && valEmail === 0 && valUser === 0 && valMob === 0 && valPassRegister === 0)
184
    {
185
        var q = {
186
            "name": name,
187
            "email": email,
188
            "username": username,
189
            "mob": mob,
190
            "password": password
191
        };
192
        q = "q=" + JSON.stringify(q);
193
        // console.log(q);
194
        var xmlhttp = new XMLHttpRequest();
0 ignored issues
show
Bug introduced by
The variable XMLHttpRequest seems to be never declared. If this is a global, consider adding a /** global: XMLHttpRequest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
195
        xmlhttp.onreadystatechange = function()
196
        {
197
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
198
            {
199
                var result = JSON.parse(xmlhttp.responseText);
200
                // console.log(result);
201
                if(result["location"])
202
                {
203
                    location.href = result["location"];
204
                }
205
                $(result).each(function(index, element) {
206
                    showError(element["key"], element["value"]);
207
                });
208
            }
209
        };
210
        xmlhttp.open("POST", "ajax/validate_register.php", true);
211
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
212
        xmlhttp.send(q);
213
    }
214
    else
215
    {
216
        // alert("Please Fill correct details");
217
        $("#myModal").modal();
218
    }
219
}
220
221